<?php

class Rbac_data extends Data {
	private $alias = 'admin-yueshihui-com';
	private $tb_node_group = 'node_group';
	private $tb_role2node = 'role2node';
	private $tb_node = 'node';
	private $tb_user2role = 'user2role';
	
	private $node_cols = array('node_id', 'gid', 'name', 'target', 'controller');
	private $node_group_cols = array('gid', 'name', 'pid');
	private $user2role_cols = array('uid', 'role_id');

	/**
	 * 获取全部平台
	 */
	public function platformAll($params, $orderby = 'gid desc') {
		if(!($db = $this->connect($this->alias))) return false;
		
		return $db->select($this->tb_node_group, $this->node_group_cols, $params, $orderby);
	}
	/**
	 * 根据角色获取平台
	 * @param unknown_type $roleids
	 */
	public function role2platform($roles, $orderby = 'gid desc') {
		if(!($db = $this->connect($this->alias))) return false;
		
		$sql = 'SELECT '.implode(', ', $this->node_group_cols).' FROM '.$this->tb_node_group.' WHERE gid IN(
			SELECT pid FROM '.$this->tb_node_group.' WHERE gid IN(
			SELECT gid FROM '.$this->tb_node.' WHERE node_id IN(
			SELECT node_id FROM '.$this->tb_role2node.' WHERE role_id IN(\''.implode("','", $roles).'\')))) ORDER BY '.$orderby;
		
		return $db->query($sql, array(), Db::SQL_ALL);
	}
	/**
	 * 获取用户角色
	 * @param unknown_type $params
	 */
	public function getRole($params) {
		if(!($db = $this->connect($this->alias))) return false;
		
		return $db->select($this->tb_user2role, $this->user2role_cols, $params);
	}
	/**
	 * 获取管理树
	 * @param unknown_type $params
	 */
	public function treeAll($pid) {
		if(!($db = $this->connect($this->alias))) return false;

		$map = create_function('$elm', 'return "node.$elm";');
		$cols = array_map($map, $this->node_cols);
		array_push($cols, 'node_group.name as groupname');
		
		$sql = 'SELECT '.implode(', ', $cols).' FROM '.$this->tb_node.' LEFT JOIN '.$this->tb_node_group.' ON node.gid=node_group.gid WHERE node.is_menu="1" AND node_group.pid='.$pid;
		return $db->query($sql, array(), Db::SQL_ALL);
	}
	/**
	 * 获取用户管理树
	 * @param unknown_type $id
	 * @param unknown_type $roles
	 */
	public function role2tree($pid, $roles) {
		if(!($db = $this->connect($this->alias))) return false;
		
		$map = create_function('$elm', 'return "node.$elm";');
		$cols = array_map($map, $this->node_cols);
		array_push($cols, 'node_group.name as groupname');
		
		$sql = 'SELECT '.implode(', ', $cols).' FROM '.$this->tb_node.' LEFT JOIN '.$this->tb_node_group.' ON node.gid=node_group.gid WHERE node.is_menu="1" AND node_group.pid='.$pid.' AND node.node_id IN(SELECT node_id FROM role2node WHERE role_id IN(\''.implode("','", $roles).'\'))';
		return $db->query($sql, array(), Db::SQL_ALL);
	}
	/**
	 * 给角色分配节点
	 * @param unknown_type $roleId
	 * @param unknown_type $nodeId
	 */
	public function role2node($roleId, $nodeId) {
		if(!($db = $this->connect($this->alias))) return false;
		
		$sql[] = 'REPLACE INTO '.$this->tb_role2node . ' (role_id, node_id) VALUES';
		foreach($nodeId as $node) {
			$sql_list[] = '('.$roleId.', '.$node.')';
		}
		$sql[] = implode(', ', $sql_list);
		$sql = implode(' ', $sql);
		return $db->query($sql, array(), Db::SQL_NONE);
	}
	/**
	 * 给角色分配用户
	 * @param unknown_type $roleId
	 * @param unknown_type $nodeId
	 */
	public function role2user($roleId, $uid) {
		if(!($db = $this->connect($this->alias))) return false;
		
		$sql[] = 'REPLACE INTO '.$this->tb_user2role . ' (role_id, uid) VALUES';
		foreach($uid as $one) {
			$sql_list[] = '('.$roleId.', '.$one.')';
		}
		$sql[] = implode(', ', $sql_list);
		$sql = implode(' ', $sql);
		return $db->query($sql, array(), Db::SQL_NONE);
	}
	/**
	 * 获取角色节点
	 * @param unknown_type $roleId
	 */
	public function getNodeByRoleId($roleId) {
		if(!($db = $this->connect($this->alias))) return false;
		
		$params = array('role_id' => $roleId);
		return $db->select($this->tb_role2node, array('role_id', 'node_id'), $params);
	}
	/**
	 * 获取角色下面的用户
	 * @param unknown_type $roleId
	 */
	public function role2userAll($roleId) {
		if(!($db = $this->connect($this->alias))) return false;
		
		$params = array('role_id' => $roleId);
		return $db->select($this->tb_user2role, array('role_id', 'uid'), $params);
	}
	/**
	 * 删除角色下的用户
	 * @param unknown_type $roleId
	 */
	public function delRole2user($roleId) {
		if(!($db = $this->connect($this->alias))) return false;
		
		$params = array('role_id' => $roleId);
		return $db->delete($this->tb_user2role, $params);
	}
	/**
	 * 删除角色下的节点
	 * @param unknown_type $roleId
	 */
	public function delRole2node($roleId) {
		if(!($db = $this->connect($this->alias))) return false;
		
		$params = array('role_id' => $roleId);
		return $db->delete($this->tb_role2node, $params);
	}
}